import 'package:flutter/material.dart';

class MessageDialog extends Dialog {
	String title;
	String negativeText;
	String positiveText;
	String imageAsset;
	Widget content;
	Function onCloseEvent;
	Function onPositivePressEvent;

	MessageDialog({
		Key key,
		@required this.title,
		@required this.content,
		@required this.imageAsset,
		this.negativeText,
		this.positiveText,
		this.onPositivePressEvent,
		@required this.onCloseEvent,
	}) : super(key: key);

	@override
	Widget build(BuildContext context) {
		return new Padding(
			padding: const EdgeInsets.all(15.0),
			child: new Material(
				type: MaterialType.transparency,
				child: new Column(
					mainAxisAlignment: MainAxisAlignment.center,
					children: <Widget>[
						new Container(
							decoration: ShapeDecoration(
								color: Color(0xffffffff),
								shape: RoundedRectangleBorder(
									borderRadius: BorderRadius.all(
										Radius.circular(8.0),
									),
								),
							),
							margin: const EdgeInsets.all(12.0),
							child: new Column(
								children: <Widget>[
									new Padding(
										padding: const EdgeInsets.all(10.0),
										child: new Stack(
											alignment: AlignmentDirectional.centerEnd,
											children: <Widget>[
												new Center(
													child: new Text(
														title,
														style: new TextStyle(
															fontSize: 19.0,
														),
													),
												),
												new GestureDetector(
													onTap: this.onCloseEvent,
													child: new Padding(
														padding: const EdgeInsets.all(5.0),
														child: new Icon(
															Icons.close,
															color: Color(0xffe0e0e0),
														),
													),
												),
											],
										),
									),
									new Container(
										color: Color(0xffe0e0e0),
										height: 1.0,
									),
									new Container(
										constraints: BoxConstraints(maxHeight: 250.0),
										child: new Padding(
											padding: const EdgeInsets.all(12.0),
											child: new IntrinsicHeight(
												child: Column(
													children: <Widget>[
														Expanded(child: ClipRect(
															child: Image.asset(imageAsset),
														),flex: 4,),
														Expanded(
															child: Text("塑料瓶"),
															flex: 1,
														),
														Expanded(
															child: content,
															flex: 1,
														)
													],
												)
											),
										),
									),
									this._buildBottomButtonGroup(),
								],
							),
						),
					],
				),
			),
		);
	}

	Widget _buildBottomButtonGroup() {
		var widgets = <Widget>[];
		if (negativeText != null && negativeText.isNotEmpty) widgets.add(_buildBottomCancelButton());
		if (positiveText != null && positiveText.isNotEmpty) widgets.add(_buildBottomPositiveButton());
		return new Flex(
			direction: Axis.horizontal,
			children: widgets,
		);
	}

	Widget _buildBottomCancelButton() {
		return new Flexible(
			fit: FlexFit.tight,
			child: new FlatButton(
				onPressed: onCloseEvent,
				child: new Text(
					negativeText,
					style: TextStyle(
						fontSize: 16.0,
					),
				),
			),
		);
	}

	Widget _buildBottomPositiveButton() {
		return new Flexible(
			fit: FlexFit.tight,
			child: new FlatButton(
				onPressed: onPositivePressEvent,
				child: new Text(
					positiveText,
					style: TextStyle(
						color: Color(Colors.teal.value),
						fontSize: 16.0,
					),
				),
			),
		);
	}
}